package com.qfedu.edu.exception;

import com.qfedu.edu.result.R;
import com.qfedu.edu.result.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;


/**
 * @author xiaobobo
 * @title: GlobalExceotion
 * @projectName cd-fy-2401-third-project-parent
 * @description: TODO
 * @date 2024/9/23  14:07
 */
@ControllerAdvice
@Slf4j
public class GlobalException{

    /**
     * 这个是处理业务异常的地方
     * @param businessException
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public R handlerBusinessException(BusinessException businessException){
        R error = R.error(businessException.getCode(), businessException.getMsg());
        return error;
    }


    /**
     * 出现了SQL异常的处理方式
     * @param sqlException
     * @return
     */
    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public R handlerSQLException(SQLException sqlException){
        R error = R.error(ResponseCode.SQL_EXCEPTION);
        return error;
    }

    /**
     * 处理的是没有权限访问的时候 抛出的这个异常
     * @param accessDeniedException
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public R handlerAccessDeniedException(AccessDeniedException accessDeniedException){
        accessDeniedException.printStackTrace();
        R error = R.error(ResponseCode.NO_PERMISSION_EXCEPTION);
        return error;
    }

    /**
     * 这个就是处理咋们的系统异常的地方
     * @param err
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R handlerSystemException(Exception err){
        err.printStackTrace();
        R error = R.error(ResponseCode.SYSTEM_EXCEPTION);
        //TODO 处理未做
        return error;
    }

}
